Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct handling of StgDict/StgInfo for Structure return types #446

Merged
merged 5 commits into from
Apr 25, 2024

Conversation

freakboy3742
Copy link
Member

Fixes #444.

python/cpython#114314 replaced the StgDict structure with StgInfo; this has been released in Python 3.13.0a6.

The real fix for this is to address this upstream, rather than requiring us to work around ctypes; but:

  1. Working up this patch was helpful in understanding how the new StgInfo structure works
  2. Until such time as a patch is merged upstream, we need a workaround so that Rubicon's tests pass.

Although the patch looks like a lot of code, most of the delta is actually a re-org so that the older code will be auto removed when Py3.13 is the oldest supported version.

As a test, I've also run the full Toga testbed on macOS with this code and it worked as expected.

PR Checklist:

  • All new features have been tested
  • All new features have been documented
  • I have read the CONTRIBUTING.md file
  • I will abide by the code of conduct

Copy link
Collaborator

@dgelessus dgelessus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for taking care of this!


# The StgDictObject structure from "Modules/_ctypes/ctypes.h". This structure is
# not officially stable across Python versions, but it didn't change between being
# introduced in 2009, and being replaced in 2004/Python 3.13.0a6.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# introduced in 2009, and being replaced in 2004/Python 3.13.0a6.
# introduced in 2009, and being replaced in 2024/Python 3.13.0a6.

raise TypeError(
f"Expected a type object, not {type(tp).__module__}.{type(tp).__qualname__}"
)
return StgInfo.from_address(id(tp) + py_type_type.tp_basicsize)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the proper PyObject_GetTypeData function for this? It's declared in object.h and is part of the public C API, so unlike the ctypes-internal functions, we don't need to reimplement this one ourselves.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After obtaining the StgInfo, we should also check that its initialized field is non-zero, like the real code in CPython does.

@mhsmith
Copy link
Member

mhsmith commented Apr 16, 2024

I'm not familiar with this area, so I'll leave the reviewing to others.

@mhsmith mhsmith removed their request for review April 16, 2024 10:04
Copy link
Member

@samschott samschott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix, and the very well documented code! Pending the requested changes by @dgelessus, this looks good to me.

@freakboy3742 freakboy3742 requested review from dgelessus and removed request for dgelessus April 22, 2024 00:45
@freakboy3742
Copy link
Member Author

@dgelessus Are you OK with this updated version?

Copy link
Collaborator

@dgelessus dgelessus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM as well. Thanks!

@freakboy3742 freakboy3742 merged commit c51937c into main Apr 25, 2024
26 checks passed
@freakboy3742 freakboy3742 deleted the ctypes-update branch April 25, 2024 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ctypes_patch breaks with Python 3.13.0a6
4 participants